package com.wqnmdb.im.service.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * redis set 服务
 * 适合红包队列
 * @author 王欣
 *
 */
@Service
public class RedisSetService {
	
	private static final Logger logger = LoggerFactory.getLogger(RedisSetService.class);
	
	@Resource
	private StringRedisTemplate stringRedisTemplate;
	
	public void delKey(String key){
		logger.debug("delKey().params:" + key);
		stringRedisTemplate.delete(key);
	}
	
	/**
	 * 增加
	 * 
	 * @param key
	 * @param value
	 * @return
	 */
	public Long add(String key, String ... value) {
		return stringRedisTemplate.opsForSet().add(key, value);
	}

	/**
	 * 大小
	 * 
	 * @param key
	 * @return
	 */
	public Long size(String key) {
		return stringRedisTemplate.opsForSet().size(key);
	}
	
	/**
	 * 是否包含指定成员
	 * @param key
	 * @param value
	 * @return
	 */
	public Boolean isMember(String key,String value) {
		return stringRedisTemplate.opsForSet().isMember(key, value);
	}

	/**
	 * 随机选一个
	 * @param key
	 * @return
	 */
	public String pop(String key) {
		return stringRedisTemplate.opsForSet().pop(key);
	}
	/**
	 * 向redis里存入数据和设置缓存时间
	 * @param key
	 * @return
	 */
	public void addAndexpire(String key,String value,int time) {
		stringRedisTemplate.opsForValue().set(key, value,time, TimeUnit.HOURS);
	}

}


